/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.bean;

import com.jb.web.application.EventManager;
import com.jb.web.bean.event.UIValueChangeEvent; 
import com.jb.web.context.WebContext;

public class ValueHolderImpl extends HtmlItem implements ValueHolder{
    static final long serialVersionUID = 123123L;

    static {
        //this is the value can be desplayed when value is null
        publish(ValueHolder.class, DATA, "defaultValue");
        //this is the value can be desplayed when value is null
        publish(ValueHolder.class, DATA, "value");

        //when value is to a data, or number, this is the format string
        publish(ValueHolder.class, DATA, "valueFormat");

        //used for inline editing
        publish(ValueHolder.class, EVENT_HANDLER, "onChangeValue");
    }

    private String valueFormat;
    protected String wrap;
    private String onChangeValue;


    public void setOnChangeValue(String onChangeValue) {
        this.onChangeValue = onChangeValue;
    }

    public String getOnChangeValue() {
        return onChangeValue;
    }

    /**
     * Sets a new value to this component. It calls onChangeValue event handler
     * to allow custom code for setting values. If a onChange event handler decide
     * to deny the value change, it should set changeDenied to true on the event.
     *
     * @param ctx the runtime context.
     * @param value the new value.
     */
    public void setValue(WebContext ctx, String value) {
        String oldValue = getValue();
        if (onChangeValue != null && onChangeValue.length() > 0) {
            UIValueChangeEvent event = 
                new UIValueChangeEvent(this, ctx.getRequest(), "value", oldValue, value);
            EventManager.getInstance().handleEvent(ctx, event, onChangeValue);
            if(!event.isChangeDenied()) {
                super.setValue(ctx, value);
            }
        }else
            super.setValue(ctx, value);

    }

    public void setValueFormat(String valueFormat) {
        this.valueFormat = valueFormat;
    }

    public String getValueFormat() {
        return valueFormat;
    }

    public void setWrap(String wrap) {
        this.wrap = wrap;
    }

    public String getWrap() {
        return wrap;
    }

}
